setwd("/media/disk/ceu/docencia/materiales/estadistica/presentaciones/curso_estadistica")
Datos <- read.table("datos/datos estaturas pesos sexo.csv", header=TRUE, sep="\t", na.strings="NA", dec=".", strip.white=TRUE)

#Diagrama de dispersión de estaturas y pesos
x11()
par(cex.lab=1.2)
plot(peso ~ estatura, col="coral", main="Diagrama de dispersión de Estaturas y Pesos", xlab="Estatura (cm)", ylab="Peso (Kg)", xlim=c(150,200), pch=16, data=Datos)
segments(179,0,179,85, lty=2, col="grey")
segments(0,85,179,85, lty=2, col="grey")
text(179,87,"(179,85)")
dev.copy2eps(file="img/regresion/diagrama_dispersion_estatura_peso.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/diagrama_dispersion_estatura_peso.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(peso ~ estatura, col="coral", main="Diagrama de dispersión de Estaturas y Pesos", xlab="Estatura (cm)", ylab="Peso (Kg)", xlim=c(150,200), pch=16, data=Datos)
segments(179,0,179,85, lty=2, col="grey")
segments(0,85,179,85, lty=2, col="grey")
text(179,87,"(179,85)")
dev.off()

#Diagramas de dispersion de diferentes tipos de relaciones 
#layout(matrix(c(1,2,3,4),2,2))
x11()
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
x=runif(100,-10,10)
y=runif(100,-10,10)
plot(x,y, col="coral", main="Sin relación", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
dev.copy2eps(file="img/regresion/diagrama_dispersion_sin_relacion.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/diagrama_dispersion_sin_relacion.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, col="coral", main="Sin relación", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
dev.off()

x11()
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
y=(x+rnorm(100,0,1))^2
plot(x,y, col="coral", main="Relación parabólica", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
dev.copy2eps(file="img/regresion/diagrama_dispersion_relacion_parabolica.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/diagrama_dispersion_relacion_parabolica.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, col="coral", main="Relación parabólica", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
dev.off()

x11()
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
y=0.5*x+rnorm(100,0,1)
plot(x,y, col="coral", main="Relación lineal", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
dev.copy2eps(file="img/regresion/diagrama_dispersion_relacion_lineal.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/diagrama_dispersion_relacion_lineal.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, col="coral", main="Relación lineal", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
dev.off()

x11()
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
y=exp(0.3*x+rnorm(100,0,0.1))+rnorm(100,0,1)
plot(x,y, col="coral", main="Relación exponencial", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
dev.copy2eps(file="img/regresion/diagrama_dispersion_relacion_exponencial.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/diagrama_dispersion_relacion_exponencial.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, col="coral", main="Relación exponencial", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
dev.off()

x11()
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
x=runif(100,0,10)
y=log(x+rnorm(100,1,0.3),2)+rnorm(100,0,0.1)
plot(x,y, col="coral", main="Relación logarítmica", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
dev.copy2eps(file="img/regresion/diagrama_dispersion_relacion_logaritmica.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/diagrama_dispersion_relacion_logaritmica.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, col="coral", main="Relación logarítmica", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
dev.off()

x11()
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
x=runif(100,0.5,5)
y=1/(x+rnorm(100,0,0.1))+rnorm(100,0,0.1)
plot(x,y, col="coral", main="Relación inversa", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
dev.copy2eps(file="img/regresion/diagrama_dispersion_relacion_inversa.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/diagrama_dispersion_relacion_inversa.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, col="coral", main="Relación inversa", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
dev.off()


#Desviaciones a las medias
x11()
par(mai=c(0.7,0.7,0.5,0.5), cex.lab=1.2, mgp=c(2,0,0))
plot(peso ~ estatura, col="coral", main="", xlab=expression(italic("X")), ylab=expression(italic("Y")), xlim=c(150,200), pch=16, data=Datos, axes=F)
box()
abline(h=69.67, lty=2,col="grey")
abline(v=174.67, lty=2,col="grey")
points(174.67,69.67, pch=16, col="royalblue1")
text(174.67,-0.2, expression(bar(italic(x))))
text(172,72,expression(paste("(",bar(italic(x)),",",bar(italic(y)),")")))
arrows(183,69.67,183,93, col="royalblue1", code=3)
arrows(174.67,93,183,93, col="royalblue1", code=3)
text(185,95,expression(paste("(",x[i],",",y[j],")")))
dev.copy2eps(file="img/regresion/desviaciones.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/desviaciones.fig", onefile=TRUE, width=5, height=5, pointsize=10)
plot(peso ~ estatura, col="coral", main="", xlab=expression(italic("X")), ylab=expression(italic("Y")), xlim=c(150,200), pch=16, data=Datos, axes=F)
box()
abline(h=69.67, lty=2,col="grey")
abline(v=174.67, lty=2,col="grey")
points(174.67,69.67, pch=16, col="royalblue1")
text(174.67,-0.2, expression(bar(italic(x))))
text(172,72,expression(paste("(",bar(italic(x)),",",bar(italic(y)),")")))
arrows(183,69.67,183,93, col="royalblue1", code=3)
arrows(174.67,93,183,93, col="royalblue1", code=3)
text(185,95,expression(paste("(",x[i],",",y[j],")")))
dev.off()

#Relacion lineal creciente con cuadrantes
x11()
x=runif(100)
y=0.5*x+rnorm(100,0,0.05)
mx=mean(x)
my=mean(y)
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, col="coral", main="Relación lineal creciente", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
abline(h=my, lty=2,col="grey")
abline(v=mx, lty=2,col="grey")
dev.copy2eps(file="img/regresion/diagrama_dispersion_relacion_lineal_creciente.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/diagrama_dispersion_relacion_lineal_creciente.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, col="coral", main="Relación lineal creciente", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
abline(h=my, lty=2,col="grey")
abline(v=mx, lty=2,col="grey")
dev.off()

#Relacion lineal decreciente con cuadrantes
x11()
x=runif(100)
y=-0.5*x+rnorm(100,0,0.05)
mx=mean(x)
my=mean(y)
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, col="coral", main="Relación lineal decreciente", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
abline(h=my, lty=2,col="grey")
abline(v=mx, lty=2,col="grey")
dev.copy2eps(file="img/regresion/diagrama_dispersion_relacion_lineal_decreciente.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/diagrama_dispersion_relacion_lineal_decreciente.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, col="coral", main="Relación lineal decreciente", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
abline(h=my, lty=2,col="grey")
abline(v=mx, lty=2,col="grey")
dev.off()

#Residuos en Y
x11()
par(mai=c(0.7,0.7,0.5,0.5), cex.lab=1.2, mgp=c(2,0,0))
plot(peso ~ estatura, col="coral", main="", xlab=expression(italic("X")), ylab=expression(italic("Y")), xlim=c(150,200), pch=16, data=Datos, axes=F)
box()
modelo = lm(Datos$peso ~ Datos$estatura)
abline(modelo, lwd=2)
text(174.67,-0.2, expression(italic("x")))
arrows(188,82.54,188,93, col="royalblue1", code=3)
segments(188,0,188,93, col="grey", lty=2)
segments(0,82.54,188,82.54, col="grey", lty=2)
segments(0,93,188,93, col="grey", lty=2)
text(150.2,82.54, expression(italic(f(x[i]))))
text(188,50, expression(italic(x[i])))
text(150,93, expression(italic(y[j])))
text(186,87,expression(italic(e[ij])))
text(186,95,expression(italic(paste("(",x[i],",",y[j],")"))))
dev.copy2eps(file="img/regresion/residuos_y.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/residuos_y.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.7,0.7,0.5,0.5), cex.lab=1.2, mgp=c(2,0,0))
plot(peso ~ estatura, col="coral", main="", xlab=expression(italic("X")), ylab=expression(italic("Y")), xlim=c(150,200), pch=16, data=Datos, axes=F)
box()
modelo = lm(Datos$peso ~ Datos$estatura)
abline(modelo, lwd=2)
text(174.67,-0.2, expression(italic("x")))
arrows(188,82.54,188,93, col="royalblue1", code=3)
segments(188,0,188,93, col="grey", lty=2)
segments(0,82.54,188,82.54, col="grey", lty=2)
segments(0,93,188,93, col="grey", lty=2)
text(150.2,82.54, expression(italic(f(x[i]))))
text(188,50, expression(italic(x[i])))
text(150,93, expression(italic(y[j])))
text(186,87,expression(italic(e[ij])))
text(186,95,expression(italic(paste("(",x[i],",",y[j],")"))))
dev.copy2eps(file="img/regresion/residuos_y.eps", width=5, height=5, pointsize=10)
dev.off()

#Residuos en X
x11()
par(mai=c(0.7,0.7,0.5,0.5), cex.lab=1.2, mgp=c(2,0,0))
plot(peso ~ estatura, col="coral", main="", xlab=expression(italic("X")), ylab=expression(italic("Y")), xlim=c(150,200), pch=16, data=Datos, axes=F)
box()
modelo = lm(Datos$estatura ~ Datos$peso)
abline(-193.1017,1.4916, lwd=2)
text(174.67,-0.2, expression(italic("x")))
arrows(188,82.54,188,93, col="royalblue1", code=3)
segments(188,0,188,93, col="grey", lty=2)
segments(0,82.54,188,82.54, col="grey", lty=2)
segments(0,93,188,93, col="grey", lty=2)
text(150.2,82.54, expression(italic(f(x[i]))))
text(188,50, expression(italic(x[i])))
text(150,93, expression(italic(y[j])))
text(186,87,expression(italic(e[ij])))
text(186,95,expression(italic(paste("(",x[i],",",y[j],")"))))
dev.copy2eps(file="img/regresion/residuos_y.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/residuos_y.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.7,0.7,0.5,0.5), cex.lab=1.2, mgp=c(2,0,0))
plot(peso ~ estatura, col="coral", main="", xlab=expression(italic("X")), ylab=expression(italic("Y")), xlim=c(150,200), pch=16, data=Datos, axes=F)
box()
modelo = lm(Datos$peso ~ Datos$estatura)
abline(modelo, lwd=2)
text(174.67,-0.2, expression(italic("x")))
arrows(188,82.54,188,93, col="royalblue1", code=3)
segments(188,0,188,93, col="grey", lty=2)
segments(0,82.54,188,82.54, col="grey", lty=2)
segments(0,93,188,93, col="grey", lty=2)
text(150.2,82.54, expression(italic(f(x[i]))))
text(188,50, expression(italic(x[i])))
text(150,93, expression(italic(y[j])))
text(186,87,expression(italic(e[ij])))
text(186,95,expression(italic(paste("(",x[i],",",y[j],")"))))
dev.copy2eps(file="img/regresion/residuos_y.eps", width=5, height=5, pointsize=10)
dev.off()


#Rectas de regresión de estatura y peso
x11()
par(cex.lab=1.2)
plot(peso ~ estatura, col="coral", main="Rectas de regresión entre Estaturas y Pesos", xlab="Estatura (cm)", ylab="Peso (Kg)", xlim=c(150,200), pch=16, data=Datos)
modelo = lm(Datos$peso ~ Datos$estatura)
abline(-108.49,1.02, lwd=2)
abline(-207.5873,1.5873, lwd=2)
points(174.67,69.67, pch=16, col="royalblue1")
text(173,72,"(x,y)")
text(195,82,"Peso sobre Estatura")
text(185,100,"Estatura sobre peso")
dev.copy2eps(file="img/regresion/rectas_regresion.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/rectas_regresion.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(peso ~ estatura, col="coral", main="Rectas de regresión entre Estaturas y Pesos", xlab="Estatura (cm)", ylab="Peso (Kg)", xlim=c(150,200), pch=16, data=Datos)
modelo = lm(Datos$peso ~ Datos$estatura)
abline(-108.49,1.02, lwd=2)
abline(-207.5873,1.5873, lwd=2)
points(174.67,69.67, pch=16, col="royalblue1")
text(173,72,"(x,y)")
text(195,82,"Peso sobre Estatura")
text(185,100,"Estatura sobre peso")
dev.off()

#Rectas dependencia lineal perfecta
x=runif(100)
y = 0.7*x +0.15
x11()
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, ylim=c(0,1), col="coral", main="Relación lineal perfecta", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
modelo = lm(y~x)
abline(modelo, lwd=2)
text(0.7,0.4, expression(paste(italic(X)," sobre ",italic(Y), " = ", italic(Y), " sobre ", italic(X))))
dev.copy2eps(file="img/regresion/rectas_dependencia_lineal_perfecta.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/rectas_dependencia_lineal_perfecta.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
x=runif(100)
y = 0.7*x +0.15
plot(x,y, ylim=c(0,1), col="coral", main="Relación lineal perfecta", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
modelo = lm(y~x)
abline(modelo, lwd=2)
text(0.7,0.4, expression(paste(italic(X)," sobre ",italic(Y), " = ", italic(Y), " sobre ", italic(X))))
dev.off()

#Rectas independencia lineal
x=runif(100)
y=runif(100)
x11()
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, col="coral", main="Sin relación lineal", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
abline(h=mean(y), lwd=2)
abline(v=mean(x), lwd=2)
text(mean(x)+0.1,0.12, expression(paste(italic(X)," sobre ",italic(Y))))
text(0.9,mean(y)-0.05, expression(paste(italic(Y)," sobre ",italic(X))))
dev.copy2eps(file="img/regresion/rectas_dependencia_lineal_perfercta.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/rectas_independencia_lineal.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.5,0.5,0.5,0.5), cex.lab=1.2, mgp=c(1,0,0))
plot(x,y, col="coral", main="Sin relación lineal", xlab=expression(italic("X")), ylab=expression(italic("Y")), pch=16, axes=F)
box()
abline(h=mean(y), lwd=2)
abline(v=mean(x), lwd=2)
text(mean(x)+0.1,0.12, expression(paste(italic(X)," sobre ",italic(Y))))
text(0.9,mean(y)-0.05, expression(paste(italic(Y)," sobre ",italic(X))))
dev.copy2eps(file="img/regresion/rectas_independencia_lineal.eps", width=5, height=5, pointsize=10)
dev.off()


#Desviaciones a las medias
x11()
par(mai=c(0.7,0.7,0.5,0.5), cex.lab=1.2, mgp=c(2,0,0))
plot(peso ~ estatura,col="coral", main="", xlab=expression(italic("X")), ylab=expression(italic("Y")), xlim=c(150,205), pch=16, data=Datos, axes=F)
box()
text(185,95,expression(paste("(",x[i],",",y[j],")")))
segments(0,93,188,93, col="grey", lty=2)
text(150,93,expression(italic(y[j])))
segments(188,0,188,93, col="grey", lty=2)
text(188,48, expression(italic(x[i])), pos=3, offset=0.5)
abline(h=69.67, lty=2, col="grey")
text(150,69.67,expression(italic(e(y))))
arrows(155,69.67,155,93, col="royalblue1", code=3)
text(155,82,"Variabilidad total", pos=4, offset=1)
text(155,79,expression(italic(y[j]-e(y))), pos=4, offset=1)
modelo = lm(Datos$peso~Datos$estatura)
abline(modelo,lwd=2)
text(195,100, expression(paste(italic(Y)," sobre ",italic(X))))
segment(0,coefficients(modelo)[2]*188+coefficients(modelo)[1],188,coefficients(modelo)[2]*188+coefficients(modelo)[1], col="grey", lty=2)
text(150,coefficients(modelo)[2]*188+coefficients(modelo)[1],expression(italic(f(x[i]))))
arrows(188,69.67,188,coefficients(modelo)[2]*188+coefficients(modelo)[1], col="royalblue1", code=3)
text(188,78,"Variabilidad explicada", pos=4, offset=1)
text(188,75,expression(italic(f(x[i])-e(x))), pos=4, offset=1)
arrows(188,coefficients(modelo)[2]*188+coefficients(modelo)[1],188,93, col="royalblue1", code=3)
text(188,89,"Variabilidad no explicada", pos=2, offset=1)
text(188,86,expression(paste(italic(e[i][j]),"=",italic(y[j]-f(x[i])))), pos=2, offset=1)
dev.copy2eps(file="img/regresion/descomposicion_variabilidad.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/descomposicion_variabilidad.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(0.7,0.7,0.5,0.5), cex.lab=1.2, mgp=c(2,0,0))
plot(peso ~ estatura,col="coral", main="", xlab=expression(italic("X")), ylab=expression(italic("Y")), xlim=c(150,205), pch=16, data=Datos, axes=F)
box()
text(185,95,expression(paste("(",x[i],",",y[j],")")))
segments(0,93,188,93, col="grey", lty=2)
text(150,93,expression(italic(y[j])))
segments(188,0,188,93, col="grey", lty=2)
text(188,48, expression(italic(x[i])), pos=3, offset=0.5)
abline(h=69.67, lty=2, col="grey")
text(150,69.67,expression(italic(e(y))))
arrows(155,69.67,155,93, col="royalblue1", code=3)
text(155,82,"Variabilidad total", pos=4, offset=1)
text(155,79,expression(italic(y[j]-e(y))), pos=4, offset=1)
modelo = lm(Datos$peso~Datos$estatura)
abline(modelo,lwd=2)
text(195,100, expression(paste(italic(Y)," sobre ",italic(X))))
segment(0,coefficients(modelo)[2]*188+coefficients(modelo)[1],188,coefficients(modelo)[2]*188+coefficients(modelo)[1], col="grey", lty=2)
text(150,coefficients(modelo)[2]*188+coefficients(modelo)[1],expression(italic(f(x[i]))))
arrows(188,69.67,188,coefficients(modelo)[2]*188+coefficients(modelo)[1], col="royalblue1", code=3)
text(188,78,"Variabilidad explicada", pos=4, offset=1)
text(188,75,expression(italic(f(x[i])-e(x))), pos=4, offset=1)
arrows(188,coefficients(modelo)[2]*188+coefficients(modelo)[1],188,93, col="royalblue1", code=3)
text(188,89,"Variabilidad no explicada", pos=2, offset=1)
text(188,86,expression(paste(italic(e[i][j]),"=",italic(y[j]-f(x[i])))), pos=2, offset=1)
dev.off()


Datos <- read.table("datos/datos_horas_bacterias.csv", header=TRUE, sep="\t", na.strings="NA", dec=".", strip.white=TRUE)
#Diagrama de dispersión de horas y bacterias
x11()
par(cex.lab=1.2)
plot(bacterias ~ horas, col="coral", main="Diagrama de dispersión de Horas y Bacterias", xlab="Horas", ylab="Bacterias", pch=16, data=Datos)
dev.copy2eps(file="img/regresion/diagrama_dispersion_horas_bacterias.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/diagrama_dispersion_horas_bacterias.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(bacterias ~ horas, col="coral", main="Diagrama de dispersión de Horas y Bacterias", xlab="Horas", ylab="Bacterias", pch=16, data=Datos)
dev.off()

x11()
par(cex.lab=1.2)
plot(log(bacterias) ~ horas, col="coral", main="Diagrama de dispersión de Horas y Logaritmo de Bacterias", xlab="Horas", ylab="Log Bacterias", pch=16, data=Datos)
dev.copy2eps(file="img/regresion/diagrama_dispersion_horas_logbacterias.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/diagrama_dispersion_horas_logbacterias.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(log(bacterias) ~ horas, col="coral", main="Diagrama de dispersión de Horas y Logaritmo de Bacterias", xlab="Horas", ylab="Log Bacterias", pch=16, data=Datos)
dev.off()

#Regresion exponencial horas bacterias
x11()
par(cex.lab=1.2)
plot(bacterias ~ horas, col="coral", main="Regresión exponencial Bacterias sobre Horas", xlab="Horas", ylab="Bacterias", pch=16, data=Datos)
modelo = lm(log(Datos$bacterias)~Datos$horas)
curve(exp(coefficients(modelo)[1])*exp(coefficients(modelo)[2]*x), add=T, lwd=2)
text(4,300,expression(italic(r^2 == 0.99)))
dev.copy2eps(file="img/regresion/regresion_exponencial_horas_bacterias.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/regresion_exponencial_horas_bacterias.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(bacterias ~ horas, col="coral", main="Regresión exponencial Bacterias sobre Horas", xlab="Horas", ylab="Bacterias", pch=16, data=Datos)
curve(exp(coefficients(modelo)[1])*exp(coefficients(modelo)[2]*x), add=T, lwd=2)
text(4,300,expression(italic(r^2 == 0.99)))
dev.off()

#Regresion lineal horas bacterias
x11()
par(cex.lab=1.2)
plot(bacterias ~ horas, col="coral", main="Regresión lineal Bacterias sobre Horas", xlab="Horas", ylab="Bacterias", pch=16, data=Datos)
modelo = lm(Datos$bacterias~Datos$horas)
abline(modelo, lwd=2)
text(4,300,expression(italic(r^2 == 0.85)))
dev.copy2eps(file="img/regresion/regresion_lineal_horas_bacterias.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/regresion_lineal_horas_bacterias.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(cex.lab=1.2)
plot(bacterias ~ horas, col="coral", main="Regresión lineal Bacterias sobre Horas", xlab="Horas", ylab="Bacterias", pch=16, data=Datos)
abline(modelo, lwd=2)
text(4,300,expression(italic(r^2 == 0.85)))
dev.off()


#Recta de regresión sobre una relación parabólica
x=runif(100,0,10)
z=x+rnorm(100,0,0.1)
y=0.25*z^2-2.5*z+8+rnorm(100,0,0.2)
x11()
par(mai=c(1,1,0.5,0.5), cex.lab=1.2)
plot(y~x, col ="coral", xlab=expression(italic(X)), ylab=expression(italic(Y)), pch=16)
modelo = lm(y~x)
abline(modelo, lwd=2)
coeffs <- floor(modelo$coeff*100)/100
plusorminus <- c("+")
if (coeffs[1] < 0 ) plusorminus <- c("")
text(5,7,paste("y=",coeffs[2],"x",plusorminus,coeffs[1]))
text(5,6.5,paste("r2 =",floor(summary(modelo)$r.squared*100)/100))
dev.copy2eps(file="img/regresion/recta_regresion_relacion_parabolica.eps", width=5, height=5, pointsize=10)
dev.off()


xfig(file="img/regresion/recta_regresion_relacion_parabolica.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(1,1,0.5,0.5), cex.lab=1.2)
plot(y~x, col ="coral", xlab=expression(italic(X)), ylab=expression(italic(Y)), pch=16)
modelo = lm(y~x)
abline(modelo, lwd=2)
coeffs <- floor(modelo$coeff*100)/100
plusorminus <- c("+")
if (coeffs[1] < 0 ) plusorminus <- c("")
text(5,7,paste("y=",coeffs[2],"x",plusorminus,coeffs[1]))
text(5,6.5,paste("r2 =",floor(summary(modelo)$r.squared*100)/100))
dev.off()

#Regresión parabólica
x11()
par(mai=c(1,1,0.5,0.5), cex.lab=1.2)
plot(y~x, col ="coral", xlab=expression(italic(X)), ylab=expression(italic(Y)), pch=16)
modelo = lm(y~x+I(x^2))
curve(coefficients(modelo)[1]+coefficients(modelo)[2]*x+coefficients(modelo)[3]*x^2, add=T, lwd=2)
coeffs <- floor(modelo$coeff*100)/100
plusorminus <- c("+")
if (coeffs[1] < 0 ) plusorminus <- c("")
text(5,7,paste("y=",coeffs[3],"x^2",coeffs[2],"x",plusorminus,coeffs[1]))
text(5,6.5,paste("r2 =",floor(summary(modelo)$r.squared*100)/100))
dev.copy2eps(file="img/regresion/regresion_parabolica.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/regresion_parabolica.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(1,1,0.5,0.5), cex.lab=1.2)
plot(y~x, col ="coral", xlab=expression(italic(X)), ylab=expression(italic(Y)), pch=16)
modelo = lm(y~x+I(x^2))
curve(coefficients(modelo)[1]+coefficients(modelo)[2]*x+coefficients(modelo)[3]*x^2, add=T, lwd=2)
coeffs <- floor(modelo$coeff*100)/100
plusorminus <- c("+")
if (coeffs[1] < 0 ) plusorminus <- c("")
text(5,7,paste("y=",coeffs[3],"x^2",coeffs[2],"x",plusorminus,coeffs[1]))
text(5,6.5,paste("r2 =",floor(summary(modelo)$r.squared*100)/100))
dev.off()



#Recta de regresión sin datos atípicos
x=runif(9,0,10)
y=-0.5*x+5+rnorm(9,0,0.2)
x11()
par(mai=c(1,1,0.5,0.5), cex.lab=1.2)
plot(y~x, col ="coral", main="Recta de regresión sin datos atípicos", xlim=c(0,10), ylim=c(0,10), xlab=expression(italic(X)), ylab=expression(italic(Y)), pch=16)
points(9,8,pch=X,col="royalblue")
modelo = lm(y~x)
abline(modelo, lwd=2)
coeffs <- floor(modelo$coeff*100)/100
plusorminus <- c("+")
if (coeffs[1] < 0 ) plusorminus <- c("")
text(5,7,paste("y=",coeffs[2],"x",plusorminus,coeffs[1]))
text(5,6.5,paste("r2 =",floor(summary(modelo)$r.squared*100)/100))
dev.copy2eps(file="img/regresion/recta_regresion_sin_datos_atipicos.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/recta_regresion_sin_datos_atipicos.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(1,1,0.5,0.5), cex.lab=1.2)
plot(y~x, col ="coral", main="Recta de regresión sin datos atípicos", xlim=c(0,10), ylim=c(0,10), xlab=expression(italic(X)), ylab=expression(italic(Y)), pch=16)
points(9,8,pch=X,col="royalblue")
modelo = lm(y~x)
abline(modelo, lwd=2)
coeffs <- floor(modelo$coeff*100)/100
plusorminus <- c("+")
if (coeffs[1] < 0 ) plusorminus <- c("")
text(5,7,paste("y=",coeffs[2],"x",plusorminus,coeffs[1]))
text(5,6.5,paste("r2 =",floor(summary(modelo)$r.squared*100)/100))
dev.off()



#Recta de regresión con datos atípicos
x=c(x,9)
y=c(y,8)
x11()
par(mai=c(1,1,0.5,0.5), cex.lab=1.2)
plot(y~x, col ="coral", main="Recta de regresión con datos atípicos", xlim=c(0,10), ylim=c(0,10), xlab=expression(italic(X)), ylab=expression(italic(Y)), pch=16)
modelo = lm(y~x)
summary(modelo)
abline(modelo, lwd=2)
coeffs <- floor(modelo$coeff*100)/100
plusorminus <- c("+")
if (coeffs[1] < 0 ) plusorminus <- c("")
text(5,7,paste("y=",coeffs[2],"x",plusorminus,coeffs[1]))
text(5,6.5,paste("r2 =",floor(summary(modelo)$r.squared*100)/100))
dev.copy2eps(file="img/regresion/recta_regresion_con_datos_atipicos.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/recta_regresion_con_datos_atipicos.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(1,1,0.5,0.5), cex.lab=1.2)
plot(y~x, col ="coral", main="Recta de regresión con datos atípicos", xlim=c(0,10), ylim=c(0,10), xlab=expression(italic(X)), ylab=expression(italic(Y)), pch=16)
modelo = lm(y~x)
summary(modelo)
abline(modelo, lwd=2)
coeffs <- floor(modelo$coeff*100)/100
plusorminus <- c("+")
if (coeffs[1] < 0 ) plusorminus <- c("")
text(5,7,paste("y=",coeffs[2],"x",plusorminus,coeffs[1]))
text(5,6.5,paste("r2 =",floor(summary(modelo)$r.squared*100)/100))
dev.off()


#Diagrama de regresión con datos atípicos
x11()
par(mai=c(1,1,0.5,0.5), cex.lab=1.2)
plot(y~x, col ="coral", main="Diagrama de dispersión con datos atípicos", xlim=c(0,10), ylim=c(0,10), xlab=expression(italic(X)), ylab=expression(italic(Y)), pch=16)
text(9,8.3,"Dato atípico")
dev.copy2eps(file="img/regresion/diagrama_dispersion_con_datos_atipicos.eps", width=5, height=5, pointsize=10)
dev.off()

xfig(file="img/regresion/diagrama_dispersion_con_datos_atipicos.fig", onefile=TRUE, width=5, height=5, pointsize=10)
par(mai=c(1,1,0.5,0.5), cex.lab=1.2)
plot(y~x, col ="coral", main="Diagrama de dispersión con datos atípicos", xlim=c(0,10), ylim=c(0,10), xlab=expression(italic(X)), ylab=expression(italic(Y)), pch=16)
text(9,8.3,"Dato atípico")
dev.off()
